//
//  CYShopCartBottomBtn.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/7/31.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYShopCartBottomBtn.h"
@interface CYShopCartBottomBtn(){
    CYShopCartBottomBtnBlock _myblock;
}
@property (nonatomic, strong) CYButton *minusBtn;//减
@property (nonatomic, strong) CYButton *plusBtn;//加
@property (nonatomic, strong) CYLabel *label;//
@end
@implementation CYShopCartBottomBtn
- (instancetype)initWithColor:(UIColor *)color block:(CYShopCartBottomBtnBlock)block{
    self = [super initWithColor:color];
    if (self) {
        _myblock = block;
        _isChangeTimes = YES;
        _times = 1;
        self.layer.masksToBounds = YES;
        self.layer.borderColor = ColorHex(0x888888).CGColor;
        self.layer.borderWidth = 0.5;
        self.layer.cornerRadius = 5.0;
        self.minusBtn.hidden = self.plusBtn.hidden = self.label.hidden = NO;
    }
    return self;
}

- (CYButton *)minusBtn{
    
    if (!_minusBtn) {
        _minusBtn = [[CYButton alloc]initWithTitle:@"-" titleColor:ColorHex(0x333333) backgroundColor:[UIColor clearColor] cornerRadius:0.0];
        [_minusBtn addTarget:self action:@selector(actionForMinusBtn:) forControlEvents:UIControlEventTouchUpInside];
        [_minusBtn setBackgroundColor:[UIColor lightGrayColor] forstate:UIControlStateHighlighted];
        _minusBtn.titleLabel.font = [UIFont systemFontOfSize:AL(PT(40))];
        [self addSubview:_minusBtn];
        [self addLeftLine];
        
        [_minusBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.left.equalTo(self).with.offset(0);
            make.size.mas_equalTo(CGSizeMake(AL(PT(66)), AL(PT(66))));
        }];
    }
    return _minusBtn;
}

- (CYButton *)plusBtn{
    
    if (!_plusBtn) {
        _plusBtn = [[CYButton alloc]initWithTitle:@"+" titleColor:ColorHex(0x333333) backgroundColor:[UIColor clearColor] cornerRadius:0.0];
        [_plusBtn addTarget:self action:@selector(actionForPlusBtn:) forControlEvents:UIControlEventTouchUpInside];
        [_plusBtn setBackgroundColor:[UIColor lightGrayColor] forstate:UIControlStateHighlighted];
        _plusBtn.titleLabel.font = [UIFont systemFontOfSize:AL(PT(40))];
        [self addSubview:_plusBtn];
        [self addRightLine];
        
        [_plusBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.right.equalTo(self.mas_right).with.offset(0);
            make.size.mas_equalTo(CGSizeMake(AL(PT(66)), AL(PT(66))));
        }];
    }
    return _plusBtn;
}

- (CYLabel *)label{
    
    if (!_label) {
        _label = [[CYLabel alloc]initWithText:[NSString stringWithFormat:@"%d",_times] size:AL(PT(28)) textcolor:ColorHex(0x333333)];
        _label.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_label];
        
        [_label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self);
            make.size.mas_equalTo(_label.bounds.size);
        }];
    }
    return _label;
}

- (void)setTimes:(int)times{
    if (_times != times) {
        _times = times;
        _label.text = [NSString stringWithFormat:@"%d",_times];
        [_label mas_updateConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(_label.bounds.size);
        }];
    }

}
- (void)addLeftLine{
    
    CYView *line = [[CYView alloc]initWithColor:ColorHex(0x888888)];
    [_minusBtn addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_minusBtn.mas_right).with.offset(0);
        make.centerY.equalTo(_minusBtn);
        make.size.mas_equalTo(CGSizeMake(0.5, AL(PT(66))));
    }];
}

- (void)addRightLine{
    
    CYView *line = [[CYView alloc]initWithColor:ColorHex(0x888888)];
    [_plusBtn addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_plusBtn.mas_left).with.offset(0);
        make.centerY.equalTo(_plusBtn);
        make.size.mas_equalTo(CGSizeMake(0.5, AL(PT(66))));
    }];
}

//减
- (void)actionForMinusBtn:(CYButton *)sender{
    if (_isChangeTimes && _times > 1)
        self.times = _times - 1;
    _myblock(-1);
    
}

//加
- (void)actionForPlusBtn:(CYButton *)sender{
    if (_isChangeTimes)
        self.times = _times + 1;
    _myblock(1);
    
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
